unit Unit26; 

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DbCtrls, sqldb, db, Unit1, Unit27;

type

  { TForm26 }

  TForm26 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Datasource1: TDatasource;
    DBComboBox1: TDBComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow;
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  Form26: TForm26; 

implementation

{ TForm26 }

procedure TForm26.Button1Click(Sender: TObject);
begin
  Form27.ShowModal;
  Edit1.Text := Form27.codigo_producto;
end;

procedure TForm26.Button2Click(Sender: TObject);
var
  QueryString : String;
  
begin
  if (Edit1.Text <> '') and (DBComboBox1.Text <> '') and (Edit2.Text <> '') then
  begin
    QueryString := 'insert into compra(cmp_producto, cmp_proveedor, cmp_cantidad, cmp_sucursal) values('
      + Edit1.Text + ', (select prv_cdg from proveedor where prv_nombre = ''' + DBComboBox1.Text
      + '''), ' + Edit2.Text + ', ' + registro.codigo_sucursal + ')';
      
    Form1.PQConnection1.ExecuteDirect('begin work;');
    Form1.PQConnection1.ExecuteDirect(QueryString);
    Form1.PQConnection1.ExecuteDirect('commit work;');

    ShowMessage('ALTA EXITOSA');
    FormShow;
  end
  else
    ShowMessage('ERROR EN EL INGRESO DE DATOS');
end;

procedure TForm26.Button3Click(Sender: TObject);
begin
  Close;
end;

procedure TForm26.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;
end;

procedure TForm26.FormShow;
begin
  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
  SQLQuery1.SQL.Text := 'select prv_nombre from proveedor order by prv_nombre';

  SQLQuery1.Open;
  DBComboBox1.DataField := 'prv_nombre';
  
  DBComboBox1.Clear;
  
  while not SQLQuery1.EOF do
  begin
    DBComboBox1.Items.Add(SQLQuery1.Fields[0].AsString);
    SQLQuery1.Next;
  end;
  
  Edit1.Text := '';
  DBComboBox1.Text := '';
  Edit2.Text := '';
  
  Edit1.SetFocus;
end;

initialization
  {$I unit26.lrs}

end.

